<template>
  <el-aside ref="aside" width="initial" :class="['aside-wrap',{'aside-wrap-nomargin':!isCollapse}]">
    <div v-if="isCollapse">
      <!-- <el-tree
        ref="tree"
        class="filter-tree"
        :data="dataList"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
      >
        <span slot-scope="{ node, data }" class="custom-tree-node">
          <span>{{ node.label }}</span>
          <span>
            <i class="el-icon-plus" @click="() => handleAdd(node,data)" />
            <i v-if="data.id !== 1" class="el-icon-more" @click="() => handleMore(node,data)" />
          </span>
        </span>
      </el-tree> -->
      <q-tree :data="data" placeholder="搜索需求分类名称"></q-tree>
    </div>
    <div class="need-toggle" @click="handleToggle">
      <i class="el-icon-caret-left toggle-icon" />
    </div>
    <Need-Type-Create :dialog-visible-need="dialogVisibleNeed" @closeDialog="closeDialog" />
  </el-aside>
</template>

<script>
import NeedTypeCreate from './NeedTypeCreate';
import QTree from '@/components/QTree'
export default {
  components: { NeedTypeCreate, QTree },
  props: {
    isCollapse: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      filterText: '',
      asideWidth: 0,
      dialogVisibleNeed: false, // 创建需求分类
      data: [
        {
          id: 1,
          label: '所有需求',
          children: [
            {
              id: 4,
              label: '未分类需求',
              children: [
                {
                  id: 9,
                  label: '需求1',
                  icon: 'el-icon-document',
                },
                {
                  id: 10,
                  label: '需求2',
                  icon: 'el-icon-document',
                },
              ],
            },
            {
              id: 2,
              label: '大数据需求',
              children: [
                {
                  id: 6,
                  label: '需求1',
                  icon: 'el-icon-document',
                },
                {
                  id: 11,
                  label: '需求2',
                  icon: 'el-icon-document',
                },
              ],
            },
            {
              id: 5,
              label: '物联网需求',
              children: [
                {
                  id: 8,
                  label: '需求1',
                  icon: 'el-icon-document',
                },
                {
                  id: 12,
                  label: '需求2',
                  icon: 'el-icon-document',
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  mounted() {
    this.getAsideWidth();
  },
  methods: {
    getAsideWidth() {
      this.asideWidth = this.$refs.aside.$el.offsetWidth;
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleToggle() {
      this.getAsideWidth();
      this.$emit('toggleHandler', this.asideWidth);
    },
    handleAdd(data) {
      this.dialogVisibleNeed = true;
    },
    closeDialog() {
      this.dialogVisibleNeed = false;
    },
  },
};
</script>

<style scoped lang='scss'>
@import '../../../styles/main.css';
.aside-wrap {
  height: 100%;
  width: 240px !important;
  padding: 8px;
  box-sizing: border-box;
  color: #333;
  font-size: 14px;
  background: #fff;
  position: fixed;
  left: 0;
  top: 120px;
  .need-toggle {
    width: 9px;
    height: 64px;
    background-color: pink;
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
  }
  // .need-type {
  //   margin-bottom: 20px;
  // }
  .toggle-icon {
    line-height: 64px;
  }
}
.aside-wrap-nomargin {
  width: 0 !important;
}
/deep/ .custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
/deep/ .el-tree-node__content {
  margin-bottom: 10px;
}
</style>
